package com.zy.algorithm.v3.sort

import com.zy.view.DIYSortView
import com.zy.view.DIYSortView.NodeLineStep
import com.zy.utils.data.auto.AutoIndexElement
import com.zy.utils.data.auto.AutoNodeStep
import com.zy.utils.data.auto.Coordinate
import java.util.ArrayList

//堆排序
class HeapSortFragment : BaseV3SortFragment() {

    override fun setSortData(diy_v4: DIYSortView) {
        val array = intArrayOf(
            10, 1, 2, 5, 14, 13, 11, 9, 6, 12, 15, 3, 7, 8, 4
        )

        //DataBuildUtils.getRandomSortArrayV2(array);
        diy_v4.setD(120)
        diy_v4.create(2, 1, array, true)
        //diy_v4.addAss(d, 2, 1, "V", per);
    }

    //自动动画
    var hasMove = false
    override fun autoMove(diy_v4: DIYSortView) {
        if (hasMove) {
            addAssistLineList(diy_v4)
            return
        }
        hasMove = true
        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()
        list.apply {
            val baseX = 10
            add(AutoNodeStep(listOf(AutoIndexElement(0, Coordinate(baseX, 2)))))
            add(AutoNodeStep(listOf(AutoIndexElement(1, Coordinate(baseX - 4, 3)))))
            add(AutoNodeStep(listOf(AutoIndexElement(2, Coordinate(baseX + 4, 3)))))
            add(AutoNodeStep(listOf(AutoIndexElement(3, Coordinate(baseX - 6, 4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(4, Coordinate(baseX - 2, 4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(5, Coordinate(baseX + 2, 4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(6, Coordinate(baseX + 6, 4)))))
            val depth4 = 6
            add(AutoNodeStep(listOf(AutoIndexElement(7, Coordinate(baseX - 7, depth4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(8, Coordinate(baseX - 5, depth4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(9, Coordinate(baseX - 3, depth4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(10, Coordinate(baseX - 1, depth4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(11, Coordinate(baseX + 1, depth4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(12, Coordinate(baseX + 3, depth4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(13, Coordinate(baseX + 5, depth4)))))
            add(AutoNodeStep(listOf(AutoIndexElement(14, Coordinate(baseX + 7, depth4)))))
        }

        diy_v4.autoMove(list)
    }

    var hasAddLine = false
    private fun addAssistLineList(diy_v4: DIYSortView) {
        if (hasAddLine) {
            return
        }
        hasAddLine = true
        val list: MutableList<NodeLineStep> = ArrayList<NodeLineStep>()
        for (i in 0..6) {
            list.add(NodeLineStep(i, i * 2 + 1))
            list.add(NodeLineStep(i, i * 2 + 2))
        }
        diy_v4.autoAddAssistLine(list)
    }
}